package cn.kgc.jinxin.clothesstore.dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import cn.kgc.jinxin.clothesstore.dao.AddressDao;
import cn.kgc.jinxin.clothesstore.dao.BaseDao;
import cn.kgc.jinxin.clothesstore.entity.Address;
import cn.kgc.jinxin.clothesstore.entity.User;

public class AddressDaoImpl implements AddressDao {

	@Override
	public List<Address> selectAll() throws Exception {
		List<Address> addressList = new ArrayList<>();
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select * from address";
			preparedStatement = connection.prepareStatement(sql);
			resultSet = preparedStatement.executeQuery();
			Address address;
			while (resultSet.next()) {
				address = new Address();
				address.setReceiver(resultSet.getString(1));
				address.setReceiverPhone(resultSet.getString(2));
				address.setReceiverAddress(resultSet.getString(3));
				address.setUser(new User(resultSet.getString(4)));
				address.setReceiverdefault(resultSet.getString(5).charAt(0));
				addressList.add(address);
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return addressList;
	}

	@Override
	public Address selectByName(String receiver, String receiverPhone, String receiverAddress) throws Exception {
		Address address = null;
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		ResultSet resultSet = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "select * from address where receiver=? and receiverphone=? and receiveraddress=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, receiver);
			preparedStatement.setString(2, receiverPhone);
			preparedStatement.setString(3, receiverAddress);
			resultSet = preparedStatement.executeQuery();
			if (resultSet.next()) {
				address = new Address();
				address.setReceiver(resultSet.getString(1));
				address.setReceiverPhone(resultSet.getString(2));
				address.setReceiverAddress(resultSet.getString(3));
				address.setUser(new User(resultSet.getString(4)));
				address.setReceiverdefault(resultSet.getString(5).charAt(0));
			} 
		} finally {
			BaseDao.close(connection, preparedStatement, resultSet);
		}
		return address;
	}

	@Override
	public void insert(Address address) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "insert into category VALUES (?,?,?,?,?)";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, address.getReceiver());
			preparedStatement.setString(2, address.getReceiverPhone());
			preparedStatement.setString(3, address.getReceiverAddress());
			preparedStatement.setString(4, address.getUser().getUserName());
			preparedStatement.setString(5, address.getReceiverdefault()+"");
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}		
	}

	@Override
	public void update(Address address,String newReceiverAddress) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "update receiver set receiveraddress=? where receiver=? and receiverPhone=? and receiver receiveraddress=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, newReceiverAddress);
			preparedStatement.setString(2, address.getReceiver());
			preparedStatement.setString(3, address.getReceiverPhone());
			preparedStatement.setString(4, address.getReceiverAddress());
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}

	}

	@Override
	public void deleteByName(String receiver, String receiverPhone, String receiverAddress) throws Exception {
		Connection connection = null;
		PreparedStatement preparedStatement = null;
		try {
			connection = BaseDao.getConnection();
			String sql = "delete from address where receiver=? and receiverPhone=? and receiver receiveraddress=?";
			preparedStatement = connection.prepareStatement(sql);
			preparedStatement.setString(1, receiver);
			preparedStatement.setString(2, receiverPhone);
			preparedStatement.setString(3, receiverAddress);
			preparedStatement.executeUpdate();
		} finally {
			BaseDao.close(connection, preparedStatement, null);
		}
		

	}

}
